package com.yuntsg.nnsfcp.config;


import com.yuntsg.nnsfcp.common.intercepter.AdminInterceptor;
import com.yuntsg.nnsfcp.common.intercepter.ApiSignatureInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Resource
    AdminInterceptor adminInterceptor;
    @Resource
    HttpServletRequest request;
    @Resource
    private ApiSignatureInterceptor apiSignatureInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //解决静态资源无法访问问题
//        registry.addResourceHandler(new String[]{"static/**"}).addResourceLocations(new String[]{"classpath:static/"});

        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
//        String serverPath = request.getServletContext().getRealPath("");
//        System.out.println(serverPath);
//        registry.addResourceHandler("/upload/**").addResourceLocations(serverPath + "/upload");

//        registry
//                .addResourceHandler("/result/**")
//                .addResourceLocations("file:" + "/data1/servers/backend/tomcat9-fundruijin-6068/webapps/ROOT/template/");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {

//        registry.addMapping("/**")
//                //设置允许跨域请求的域名
//                .allowedOrigins("*")
//                //是否允许证书 不再默认开启
//                .allowCredentials(true)
//                //设置允许的方法
//                .allowedMethods("*")
//                .allowedHeaders("*")
//                //跨域允许时间
//                .maxAge(3600);

        //跨域访问
//        registry.addMapping("/**").allowedOrigins(new String[]{"*"}).allowedMethods(new String[]{"GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}).allowCredentials(true);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //拦截所有请求 addInterceptor需要一个实现HandlerInterceptor接口的拦截器实例，addPathPatterns方法用于设置拦截器的过滤路径规则
//        registry.addInterceptor(new AdminInterceptor()).addPathPatterns(new String[]{"/**"});
        registry.addInterceptor(adminInterceptor).addPathPatterns()
                // 多个拦截器组成一个拦截器链
                // addPathPatterns 用于添加拦截规则
                // excludePathPatterns 用户排除拦截
                .addPathPatterns("/console/**")
                .addPathPatterns("/sys/**")
                .addPathPatterns("/back_edit_front_user/**")
                .addPathPatterns("/back/**")
                .excludePathPatterns("/sys/user/get-all-roles");

        //ljx
        registry.addInterceptor(apiSignatureInterceptor)
                //我的文献
                .addPathPatterns("/literature/**")
                //用户数据
                .addPathPatterns("/user/**")
                .addPathPatterns("/nsfc/**")
                .addPathPatterns("/front/**")
                .addPathPatterns("/his-fund-search/**")
                //订单数据
                .addPathPatterns("/order/**")
                //截图
                .addPathPatterns("/screen/**")
                //留言
                .addPathPatterns("/front_message/**");

    }

}



